//
//  SendPhotoVC.swift
//  mmda
//
//  Created by 李陆涛 on 2018/8/3.
//  Copyright © 2018年 mmda. All rights reserved.
//

import UIKit
import IQKeyboardManagerSwift
import SnapKit
import HXPhotoPicker

class SendPhotoVC: BaseVC {


    private var scrollView:UIScrollView!
    private var photoView: PhotoView!
    private var inputTV: IQTextView!
    
    private var visableLabel: UILabel!

    
    private var viewModel: SendPhotoViewModel!
    
    public convenience init(models:[HXPhotoModel]?,type:SelectType?) {
        
        self.init()
        
        viewModel = SendPhotoViewModel()
        
        self.photoView = PhotoView(frame: CGRect(x: 16, y: 166, width: kScreenWidth - 32, height: 0), manager: viewModel.mananger)
        
        if let models = models,let type = type {
            
            viewModel.addModels(models: models, type: type)
        }
        
        
        viewModel.loadFinished = {
            
            [weak self] errmsg in
            guard let `self` = self else { return }
            
            
            if let msg = errmsg {
                
                self.showToast(msg)
            } else {
                
                self.showToast("发送成功")
                self.navigationController?.popViewController(animated: true)
                
                NotificationCenter.default.post(name: kBackMapReloadNotifationName, object: nil)
            }
        }
        
        viewModel.showToastActivity = {
            
            [weak self] success in
            guard let `self` = self else { return }
            
            self.showToastActivity(success)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white
        
        self.setupUI()
    }
    
    
    func setupUI() {
        
        self.title = "发动态"
        scrollView = UIScrollView(frame: self.view.bounds)
        scrollView.alwaysBounceVertical = true
        self.view.addSubview(scrollView)
        
        inputTV = IQTextView(frame: CGRect(x: 16, y: 16, width: kScreenWidth - 32, height: 150))

        inputTV.textColor = UIColor.black
        inputTV.placeholder = "这一刻的想法"
        self.scrollView.addSubview(inputTV)
        
        photoView.backgroundColor = UIColor.white
        photoView.delegate = self
        photoView.showAddCell = true
        photoView.previewShowDeleteButton = true
        self.scrollView.addSubview(photoView)
        
        photoView.collectionView.reloadData()
        
        photoView.choosePhotoAction = {
            
            [weak self] in
            guard let `self` = self else { return }

            PhotoAlertVC.alert(type: self.viewModel.type, maxCount: self.viewModel.max, vc: self) {
                
                [weak self] modelArray,type in
                guard let `self` = self else { return }
                
                self.viewModel.addModels(models: modelArray,type: type)
                self.photoView.refreshView()
            }
        }
        
        setupVisableView()
        
        let rightItem = UIBarButtonItem(title: "提交", style: UIBarButtonItemStyle.done, target: self, action: #selector(commitAction))
        
        self.navigationItem.rightBarButtonItem = rightItem
    }
    
    private func setupVisableView() {
        
        let visableView = UIView(frame: CGRect.zero);
        
        self.scrollView.addSubview(visableView)
        
        visableView.snp.makeConstraints {
            [weak self] maker in
            guard let `self` = self else { return }
            
            maker.top.equalTo(self.photoView.snp.bottom).offset(30)
            maker.left.equalTo(self.photoView.snp.left)
            maker.right.equalTo(self.photoView.snp.right)
            maker.height.equalTo(44)
        }
        
        let lineView1 = UIView()
        lineView1.backgroundColor = kBGColor;
        
        visableView.addSubview(lineView1)
        
        lineView1.snp.makeConstraints {
            [weak visableView] maker in
            guard let `visableView` = visableView else { return }
            
            maker.top.equalTo(visableView.snp.top)
            maker.left.equalTo(visableView.snp.left)
            maker.right.equalTo(visableView.snp.right)
            maker.height.equalTo(1)
        }
        
        let lineView2 = UIView()
        lineView2.backgroundColor = kBGColor;
        
        visableView.addSubview(lineView2)
        
        lineView2.snp.makeConstraints {
            [weak visableView] maker in
            guard let `visableView` = visableView else { return }
            
            maker.bottom.equalTo(visableView.snp.bottom)
            maker.left.equalTo(visableView.snp.left)
            maker.right.equalTo(visableView.snp.right)
            maker.height.equalTo(1)
        }
        
        
        let detailIV = UIImageView(image: #imageLiteral(resourceName: "detail").withRenderingMode(.alwaysTemplate))
        detailIV.tintColor = kContentColor
        
        visableView.addSubview(detailIV)
        
        detailIV.snp.makeConstraints {
            [weak visableView] maker in
            guard let `visableView` = visableView else { return }
    
            maker.right.equalTo(visableView.snp.right).offset(-16)
            maker.centerY.equalTo(visableView.snp.centerY)
        }
        
        visableLabel = UILabel()
        visableLabel.textColor = UIColor.black
        visableLabel.font = kFont14
        visableLabel.text = "所有人可见"
        visableLabel.sizeToFit()
        
        visableView.addSubview(visableLabel)
        
        visableLabel.snp.makeConstraints {
            [weak visableView] maker in
            guard let `visableView` = visableView else { return }
            
            maker.left.equalTo(visableView.snp.left).offset(16)
            maker.centerY.equalTo(visableView.snp.centerY)
        }
        
        visableView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(changeVisableStyle)))
    }
    
    @objc private func changeVisableStyle() {
        
        let alertVC = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        
        alertVC.addAction(UIAlertAction(title: "设为仅自己可见", style: .default, handler: {
            
            [weak self] _ in
            guard let `self` = self else { return }
            
            self.visableLabel.text = "仅自己可见"
            self.viewModel.status = 11
        }))
        
        alertVC.addAction(UIAlertAction(title: "设为所有人可见", style: .default, handler: {
            
            [weak self] _ in
            guard let `self` = self else { return }
            
            self.visableLabel.text = "所有人可见"
            self.viewModel.status = 0
        }))
        
        alertVC.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
        
        self.present(alertVC, animated: true, completion: nil)
    }
    
    @objc private func commitAction() {
        
        
        viewModel.uploadBlog(remark: self.inputTV.text)
    }
}

extension SendPhotoVC: HXPhotoViewDelegate {
    
    func photoView(_ photoView: HXPhotoView!, updateFrame frame: CGRect) {
        
        var consize = self.scrollView.contentSize
        
        consize.height = frame.origin.y + frame.size.height
        
        self.scrollView.contentSize = consize
    }
}
